package com.siyao.redvip.ui.activity;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.siyao.redvip.R;
import com.siyao.redvip.RedApplication;
import com.siyao.redvip.bean.HDxxBean;
import com.siyao.redvip.bean.TResponseBean;
import com.siyao.redvip.components.base.BaseFragmentActivity;
import com.siyao.redvip.network.NetConstans;
import com.siyao.redvip.ui.adapter.HdxxAdapter;
import com.siyao.redvip.utils.DateTimeUtil;
import com.siyao.redvip.utils.ToastUtils;
import com.siyao.redvip.utils.Track;
import com.siyao.redvip.volley.NetRequest;
import com.siyao.redvip.volley.VolleyErrorListener;
import com.siyao.redvip.volley.VolleySuccessListener;

/**
 * 活动信息列表
 * @author lenovo
 *
 */
public class HdxxActivity extends BaseFragmentActivity implements OnItemClickListener{
	
	private Intent intent;
	private NetRequest request;
	private PullToRefreshListView pullToRefreshListView;//下拉刷新listview
	private HdxxAdapter hdxxAdapter; //活动信息的数据
	private ListView actualListView;
	private List<HDxxBean> dataList;
	private int currPage;//当前页
	private int total;//总页数
	String nowDate="";
	String memberid;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitleText("活动信息");
		setLeftButton("返回");
	}

	@Override
	protected void beforeInit() {
		//初始化日期
		Date dBefore = new Date();
		Calendar calendar = Calendar.getInstance(); //得到日历
		calendar.setTime(new Date());//把当前时间赋给日历
		calendar.add(Calendar.DAY_OF_MONTH, 2);  //设置为前一天
		dBefore = calendar.getTime();   //得到前一天的时间
		nowDate=DateTimeUtil.formatDateTime(dBefore, DateTimeUtil.DF_YYYY_MM_DD);//比当天的日期顺延后2天
		memberid= RedApplication.getUser().getVolunteerId();
	}
	
	@Override
	public void ininlazeLayout() {
		View appLayout = getLayoutInflater().inflate(R.layout.activity_hdxx_list_frament, null);
		appMainView.addView(appLayout, layoutParams);
	}
	
	@Override
	public void ininlazeView() {
		pullToRefreshListView=(PullToRefreshListView) findViewById(R.id.pull_refresh_list);
		 // 设置PullToRefresh  
		pullToRefreshListView.setMode(Mode.PULL_FROM_END);
		pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
			//下拉
			@Override
			public void onPullDownToRefresh(PullToRefreshBase<ListView>  refreshView) {
				int flags = DateUtils.FORMAT_SHOW_TIME
						| DateUtils.FORMAT_SHOW_DATE
						| DateUtils.FORMAT_ABBREV_ALL;
				String label = DateUtils.formatDateTime(
						getApplicationContext(),
						System.currentTimeMillis(), flags);
				// 更新最后刷新时间
				refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
				getData(refreshView);//获取最新的信息，待定.
			}
			
			//上拉
			@Override
			public void onPullUpToRefresh(PullToRefreshBase<ListView>  refreshView) {
				int flags = DateUtils.FORMAT_SHOW_TIME
						| DateUtils.FORMAT_SHOW_DATE
						| DateUtils.FORMAT_ABBREV_ALL;
				String label = DateUtils.formatDateTime(
						getApplicationContext(),
						System.currentTimeMillis(), flags);
				// 更新最后刷新时间
				refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
				//获取数据
				getData(refreshView);
			}
		});
		
		actualListView=pullToRefreshListView.getRefreshableView();
		actualListView.setOnItemClickListener(this);
		hdxxAdapter = new HdxxAdapter(this);
	}

	@Override
	public void ininlazeData() {
		request = new NetRequest(this);
		currPage=1;
		//取活动信息
		Map<String, String> paramMap = new HashMap<String ,String>();
		
		paramMap.put("date", nowDate);
		paramMap.put("memberId", memberid);
		paramMap.put("currPage", currPage+"");
		request.doPostRequest(NetConstans.ACTIVITY_LIST, paramMap, new VolleySuccessListener(){
			@Override
			public void onResponse(String response) {
				super.onResponse(response);
				if(netBean.getStatus()==1){
					TResponseBean<HDxxBean> data=gson.fromJson(response, new TypeToken<TResponseBean<HDxxBean>>() {}.getType());
					dataList=data.getData();//活动信息
					if(!dataList.isEmpty()){
						hdxxAdapter.setmDatas(dataList);
						actualListView.setAdapter(hdxxAdapter);
					}else{
						ToastUtils.makeMesageShort(HdxxActivity.this, "今天没有活动!");	
					}
				}else{
					ToastUtils.makeMesageShort(HdxxActivity.this, netBean.getMessage());
				}
			}
		}, new VolleyErrorListener(){
			@Override
			public void onErrorResponse(VolleyError error) {
				ToastUtils.makeMesageShort(HdxxActivity.this, getString(R.string.server_error));
			}
		});
	}

	
	@Override
	public void onClick(View v) {
		super.onClick(v);
		int id =v.getId();
		switch (id) {
		case R.id.top_right_btn_image:
			intent = new Intent(this,PhotoActivity.class);
			startActivity(intent);
			finish();
			break;

		default:
			break;
		}
	}

	private void getData(final PullToRefreshBase<ListView> refreshView) {
		Map<String, String>  paramMap = new HashMap<String,String>();
		currPage+=1;
		paramMap.put("date", nowDate);
		paramMap.put("memberId", memberid);
		paramMap.put("currPage", currPage+"");
		request.doPostRequest(NetConstans.ACTIVITY_LIST, paramMap , new VolleySuccessListener(){
			@Override
			public void onResponse(String response) {
				super.onResponse(response);
				if (netBean.getStatus() == 1) {
					TResponseBean<HDxxBean> list =gson.fromJson(response, new TypeToken<TResponseBean<HDxxBean>>() {}.getType());
						List<HDxxBean> newList= list.getData();//取出新数据
						if(!newList.isEmpty()){
							//把新数据加进去
							for (int i = 0; i < newList.size(); i++) {
								HDxxBean  zx=(HDxxBean) newList.get(i);
								dataList.add(zx);
							}
							hdxxAdapter.setDataChanged(dataList);
						}else{
							currPage=currPage-1;
							ToastUtils.makeMesageShort(HdxxActivity.this, getString(R.string.server_no_data));
						}
						// 当数据加载完成，需要调用onRefreshComplete.
						refreshView.onRefreshComplete();
				}else{
					ToastUtils.makeMesageShort(HdxxActivity.this,getString(R.string.server_error));
					refreshView.onRefreshComplete();
				}
			}
		}, new VolleyErrorListener(){
			@Override
			public void onErrorResponse(VolleyError error) {
				super.onErrorResponse(error);
				ToastUtils.makeMesageShort(HdxxActivity.this,getString(R.string.server_error));
				refreshView.onRefreshComplete();
				dismissPd();
			}
		});
		
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		showPd();
		HDxxBean  hdxxBean =dataList.get(position-1);
		intent = new Intent();
		intent.putExtra("hdxxId", hdxxBean.getGet_activitycode());
		intent.putExtra("content", hdxxBean.getGet_name());
		intent.putExtra("datatime", hdxxBean.getGet_startdate());
		intent.putExtra("place", hdxxBean.getGet_address());
		intent.putExtra("manager", hdxxBean.getGet_employeename());
		intent.putExtra("telephone", hdxxBean.getGet_tel());
		intent.setClass(HdxxActivity.this, HdxxDetailsActivity.class);
		startActivity(intent);
		finish();
		dismissPd();
	}
}
